/****************************************************************************
 Copyright (c) 2012-2012 lijiejun
 Copyright (c) 2010-2012 cocos2d-x.org
 Copyright (c) 2008-2010 Ricardo Quesada
 Copyright (c) 2011      Zynga Inc.
 
 http://www.cocos2d-x.org
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

package org.cocos2d.textures
{
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.textures.Texture;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	
	import org.cocos2d.cocoa.CCSize;
	import org.cocos2d.events.AssetEvent;
	import org.cocos2d.loaders.misc.SingleFileLoader;
	import org.cocos2d.loaders.misc.parsers.ImageParser;
	import org.cocos2d.platform.CCApplication;
	import org.cocos2d.support.ccUtils;

	public class CCTexture2D
	{
		public var fileLoader : SingleFileLoader;
		
		public var texture : Texture;
		public var bitmap : BitmapData;
		
		public var texinited : Boolean;
		
		public function get inited() : Boolean
		{
			return texinited;
		}
		
		public function CCTexture2D()
		{
			fileLoader = new SingleFileLoader();
			texinited = false;
			texture = null;
			
			m_bHasPremultipliedAlpha = false;
		}
		
		public function initWithPath(path : String) : Boolean
		{
			var request : URLRequest = new URLRequest(path);
			var parser : ImageParser = new ImageParser();
			fileLoader.load(request, parser);
			fileLoader.addEventListener(AssetEvent.BITMAP_COMPLETE, onAssetComplete);
		}
		
		private function onAssetComplete(event : AssetEvent) : void
		{
			bitmap = event.asset as BitmapData;
			
			var usedBmp : BitmapData  = null;
			
			if (bitmap)
			{
				m_tContentSize.width = bitmap.width;
				m_tContentSize.height = bitmap.height;
				
				m_uPixelsWide = ccUtils.ccNextPOT(bitmap.width);
				m_uPixelsHigh = ccUtils.ccNextPOT(bitmap.height);
				
				if (bitmap.width != m_uPixelsWide || bitmap.height != m_uPixelsHigh)
				{
					var usedBmp = new BitmapData(m_uPixelsWide, m_uPixelsHigh, true, 0x00000000);
					
					var sourceRect : Rectangle;
					var destPoint : Point;
					
					sourceRect = new Rectangle(0, 0, bitmap.width, bitmap.height);
					destPoint = new Point(0, 0);
					
					usedBmp.copyPixels(bitmap, sourceRect, destPoint);
					
					m_fMaxS = m_tContentSize.width / m_uPixelsWide;
					m_fMaxT = m_tContentSize.height / m_uPixelsHigh;
				}
				else
				{
					usedBmp = bitmap;
					m_fMaxS = 1.0;
					m_fMaxT = 1.0;
				}
				
				texture = CCApplication.sharedApplication().Context.createTexture(m_uPixelsWide, m_uPixelsHigh, Context3DTextureFormat.BGRA, false);
				
				// TODO: mipmap logic
				texture.uploadFromBitmapData(usedBmp);
				
				texinited = true;
			}
		}
		
		protected var m_uPixelsWide : uint;
		
		public function get PixelsWide() : uint
		{
			return m_uPixelsWide;
		}
		
		protected var m_uPixelsHigh : uint;
		
		public function get PixelsHigh() : uint
		{
			return m_uPixelsHigh;
		}
		
		public function get Name() : Texture
		{
			return texture;
		}
		
		protected var m_fMaxS : Number;
		
		public function get MaxS() : Number
		{
			return m_fMaxS;
		}
		
		public function set MaxS(value : Number) : void
		{
			m_fMaxS = value;
		}
		
		protected var m_fMaxT : Number;
		
		public function get MaxT() : Number
		{
			return m_fMaxT;
		}
		
		public function set MaxT(value : Number) : void
		{
			m_fMaxT = value;
		}
		
		protected var m_tContentSize : CCSize = new CCSize();
		
		public function get ContentSize() : CCSize
		{
			return m_tContentSize;
		}
		
		protected var m_bHasPremultipliedAlpha : Boolean;
		
		public function hasPremultipliedAlpha() : Boolean
		{
			return m_bHasPremultipliedAlpha;
		}
	}
}